import {getStore} from './storage'
export default class myUtils {
  /**
   * 判断字符串是否为空
   * @param str
   * @returns {boolean}
   */
  static isNull(str) {
    return str == null || str.length === 0 || str === '';
  }

  /**
   *
   * @desc  判断是否为身份证号
   * @param  {String|Number} str
   * @return {Boolean}
   */
  static isIdCard(str) {
    return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str)
  }

  /**
   *
   * @desc   判断是否为手机号
   * @param  {String|Number} str
   * @return {Boolean}
   */
  static isPhoneNum(str) {
    return /^(0|86|17951)?(1[3-9][0-9])[0-9]{8}$/.test(str)
  }

  /**
   *
   * @desc   地址拼接http://
   * @param  {String|url} str
   * @return {String}
   */
  static convertHttpUrl(url) {
    if (url.substr(0, 7).toLowerCase() == "http://" || url.substr(0, 8).toLowerCase() == "https://") {
      url = url;
    } else {
      url = "http://" + url;
    }
    return url;
  }

  /**
   *
   * @desc   表单校验
   * @param  对象
   * @return msg对象
   */
  static checkFrom(obj) {
    // 用来保存所有的属性名称和值
    let props = "";
    for (let p in obj) {
      // 方法
      if (typeof(obj[p]) == "function") {
        obj[p]();
      } else {
        // p 为属性名称，obj[p]为对应属性的值
        props += p + "=" + obj[p] + "\t";
      }
    }
    // 最后显示所有的属性
    console.log(props);
  }

  /**
   *
   * @desc   获取参数名
   * @param  {String|Number} str
   * @return {String}
   */
  static GetUrlParam(paraName) {
    let url = document.location.toString();
    let arrObj = url.split("?");
    if (arrObj.length > 1) {
      let arrPara = arrObj[1].split("&");
      let arr;
      for (let i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");
        if (arr != null && arr[0] == paraName) {
          let result = arr[1];
          return result.split('#')[0];
        }
      }
      return "";
    } else {
      return "";
    }
  }

  /**
   *
   * @desc  页面去除指定参数
   * @param  {String|Number} str
   * @return {String}
   */
  static delParam(url,paramKey) {
    let urlParam = window.location.search.substr(1);   //页面参数
    let beforeUrl = url.substr(0, url.indexOf("?"));   //页面主地址（参数之前地址）
    let nextUrl = "";
    let arr = [];
    if (urlParam != "") {
      let urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
      for (let i = 0; i < urlParamArr.length; i++) {
        let paramArr = urlParamArr[i].split("="); //将参数键，值拆开
        //如果键雨要删除的不一致，则加入到参数中
        if (paramArr[0] != paramKey) {
          arr.push(urlParamArr[i]);
        }
      }
    }
    if (arr.length > 0) {
      nextUrl = "?" + arr.join("&");
    }
    url = beforeUrl + nextUrl;
    return url;
  }


  static computedMaxDate(startDate,numDay) {
    let isNull=this.isNull(numDay);
    if(isNull){
      numDay=2
    }
    let maxDate = new Date(startDate.setDate(startDate.getDate() + parseInt(numDay)));
    return maxDate;
  }

  static replaceStr(str) {
    return str.replace(/(.{4})/g, '$1 ');
  }


  static checkMobile(mobile) {
    let pattern = /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|16[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/;
    let flag = pattern.test(mobile);
    if (!flag) {
      return false;
    } else {
      return true;
    }
  }

  static checkEmail(email) {
    let pattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    let flag = pattern.test(email);
    if (!flag) {
      return false;
    } else {
      return true;
    }
  }

  static isCardNo(cardNo) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!(reg.test(cardNo))) {
      return false;
    }
    return true;
  }

  static isNumber(text) {
    let reg = /^[0-9]+$/;
    if (!(reg.test(text))) {
      return false;
    } else {
      return true;
    }
  }

  static checkUserName(name) {
    // 加上能验证少数民族名
    let reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
    if (!(reg.test(name))) {
      return false;
    } else {
      return true;
    }
  }

  static getBaseUrl() {
    let protocol = window.location.protocol + "//";
    let hostname = window.location.hostname;
    let port =  window.location.port;
    if(this.isNull(port)){
      return protocol + hostname;
    }else{
      return protocol + hostname + ":"+port;
    }

  }

  static checkPwStrong(value) {
    let strength = 0;
    if (value.length >= 6 && value.match(/[\da-zA-Z]+/)) {
      if (value.match(/\d+/)) {
        strength++;
      }
      if (value.match(/[a-z]+/)) {
        strength++;
      }
      if (value.match(/[A-Z]+/)) {
        strength++;
      }
      if (value.match(/[!@*$-_()+=&￥]+/)) {
        strength++;
      }
    }
    if (strength >= 3) {
      return true;
    }
    return false;
  }

  static initShareUrl() {
    let url1 = window.location.href.split('#')[0];
    let url2 = window.location.href.split('#/')[1];
    let path = url2.split('?')[0];
    let params = encodeURIComponent(url2.split('?')[1]);
    if(url1.indexOf("openId")!=-1){
      url1=this.delParam(url1,"openId")
    }
    let url = url1 + '&isShare=true&path=' + path + '&params=' + params;
    if(url1.indexOf("?")==-1){
      url = url1 + '?isShare=true&path=' + path + '&params=' + params;
    }
    return url;
  }
}
